package com.sxyc.weixin.m2m;

import java.util.HashMap;
import java.util.Map;

import javax.xml.soap.Name;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;


public class TerminalDetailsService implements ApiClientConstant {

	public static boolean isActive(String iccid){
		GetTerminalDetails details = new GetTerminalDetails();
		SOAPMessage message = details.GetTerminalDetail(iccid);
		SOAPEnvelope envelope;
		try {
			envelope = message.getSOAPPart().getEnvelope();
			Name terminalResponseName = envelope.createName("GetTerminalDetailsResponse", PREFIX, NAMESPACE_URI);
	        SOAPBodyElement terminalResponseElement = (SOAPBodyElement) message
	                .getSOAPBody().getChildElements(terminalResponseName).next();
//	        String terminalValue = terminalResponseElement.getTextContent();
	        Name terminals = envelope.createName("terminals", PREFIX, NAMESPACE_URI);
	        Name terminal = envelope.createName("terminal", PREFIX, NAMESPACE_URI);
	        SOAPBodyElement terminalsElement = (SOAPBodyElement) terminalResponseElement.getChildElements(terminals).next();
	        SOAPBodyElement terminalElement = (SOAPBodyElement) terminalsElement.getChildElements(terminal).next();
	        Name status = envelope.createName("status", PREFIX, NAMESPACE_URI);
	        SOAPBodyElement statusElement = (SOAPBodyElement) terminalElement.getChildElements(status).next();
	        String status1 = statusElement.getTextContent();
	        System.out.println(status1);
	        if(status1.equals("DEACTIVATED_NAME")){
	        	return false;
	        }
		} catch (SOAPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
		return true;
	}
	
	public static Map<String, String> getDetail(String iccid){
		GetTerminalDetails details = new GetTerminalDetails();
		SOAPMessage message = details.GetTerminalDetail(iccid);
		if(message != null){
			Map<String, String> map = new HashMap<String, String>();
			SOAPEnvelope envelope;
			try {
				envelope = message.getSOAPPart().getEnvelope();
				Name terminalResponseName = envelope.createName("GetTerminalDetailsResponse", PREFIX, NAMESPACE_URI);
		        SOAPBodyElement terminalResponseElement = (SOAPBodyElement) message
		                .getSOAPBody().getChildElements(terminalResponseName).next();
	//	        String terminalValue = terminalResponseElement.getTextContent();
		        Name terminals = envelope.createName("terminals", PREFIX, NAMESPACE_URI);
		        Name terminal = envelope.createName("terminal", PREFIX, NAMESPACE_URI);
		        SOAPBodyElement terminalsElement = (SOAPBodyElement) terminalResponseElement.getChildElements(terminals).next();
		        SOAPBodyElement terminalElement = (SOAPBodyElement) terminalsElement.getChildElements(terminal).next();
		        Name status = envelope.createName("status", PREFIX, NAMESPACE_URI);
		        SOAPBodyElement statusElement = (SOAPBodyElement) terminalElement.getChildElements(status).next();
		        
				map.put("status", statusElement.getTextContent());
			    Name ratePlan = envelope.createName("ratePlan", PREFIX, NAMESPACE_URI);
			    SOAPBodyElement ratePlanElement = (SOAPBodyElement) terminalElement.getChildElements(ratePlan).next();
			    map.put("ratePlan", ratePlanElement.getTextContent());
			    if("ACTIVATED_NAME".equals(map.get("status"))){
			        Name rating = envelope.createName("rating", PREFIX, NAMESPACE_URI);
			        Name endDate = envelope.createName("termEndDate", PREFIX, NAMESPACE_URI);
			        Name remain = envelope.createName("primaryDataRemaining", PREFIX, NAMESPACE_URI);
			        SOAPBodyElement ratingElement = (SOAPBodyElement) terminalElement.getChildElements(rating).next();
			        SOAPBodyElement endDateElement = (SOAPBodyElement) ratingElement.getChildElements(endDate).next();
			        SOAPBodyElement remainElement = (SOAPBodyElement) ratingElement.getChildElements(remain).next();
			        map.put("endDate", endDateElement.getTextContent().substring(0,10));
			        map.put("remain", remainElement.getTextContent());
		        }
				
		        return map;
			} catch (SOAPException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        
			return map;
		}
		return null;
	}
	
	public Map<String, String> getDetails(String iccid){
		GetTerminalDetails details = new GetTerminalDetails();
		SOAPMessage message = details.GetTerminalDetail(iccid);
		if(message != null){
			Map<String, String> map = new HashMap<String, String>();
			SOAPEnvelope envelope;
			try {
				envelope = message.getSOAPPart().getEnvelope();
				Name terminalResponseName = envelope.createName("GetTerminalDetailsResponse", PREFIX, NAMESPACE_URI);
		        SOAPBodyElement terminalResponseElement = (SOAPBodyElement) message
		                .getSOAPBody().getChildElements(terminalResponseName).next();
	//	        String terminalValue = terminalResponseElement.getTextContent();
		        Name terminals = envelope.createName("terminals", PREFIX, NAMESPACE_URI);
		        Name terminal = envelope.createName("terminal", PREFIX, NAMESPACE_URI);
		        SOAPBodyElement terminalsElement = (SOAPBodyElement) terminalResponseElement.getChildElements(terminals).next();
		        SOAPBodyElement terminalElement = (SOAPBodyElement) terminalsElement.getChildElements(terminal).next();
		        Name status = envelope.createName("status", PREFIX, NAMESPACE_URI);
		        SOAPBodyElement statusElement = (SOAPBodyElement) terminalElement.getChildElements(status).next();
				String startDate = "";
				String endDate = "";
				String totalData = "0";
				String remain = "0";
			    if("ACTIVATED_NAME".equals(statusElement.getTextContent())){
			        Name rating = envelope.createName("rating", PREFIX, NAMESPACE_URI);
			        Name startDateName = envelope.createName("termStartDate", PREFIX, NAMESPACE_URI);
			        Name endDateName = envelope.createName("termEndDate", PREFIX, NAMESPACE_URI);
			        Name totalDataName = envelope.createName("totalPrimaryIncludedData", PREFIX, NAMESPACE_URI);
			        Name remainName = envelope.createName("primaryDataRemaining", PREFIX, NAMESPACE_URI);
			        SOAPBodyElement ratingElement = (SOAPBodyElement) terminalElement.getChildElements(rating).next();
			        SOAPBodyElement startDateElement = (SOAPBodyElement) ratingElement.getChildElements(startDateName).next();
			        SOAPBodyElement endDateElement = (SOAPBodyElement) ratingElement.getChildElements(endDateName).next();
			        SOAPBodyElement totalDataElement = (SOAPBodyElement) ratingElement.getChildElements(totalDataName).next();
			        SOAPBodyElement remainElement = (SOAPBodyElement) ratingElement.getChildElements(remainName).next();
			        startDate = startDateElement.getTextContent().substring(0,10);
			        endDate = endDateElement.getTextContent().substring(0,10);
			        totalData = totalDataElement.getTextContent();
			        remain = remainElement.getTextContent();
		        }
			    map.put("sim", terminalElement.getAttribute("msisdn"));
				map.put("iccid", iccid);
				map.put("status", statusElement.getTextContent());
				map.put("startDate", startDate);
				map.put("endDate", endDate);
		        map.put("totalData", totalData);
		        map.put("remain", remain);
		        
			} catch (SOAPException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        
			return map;
		}
		return null;
	}
	
	/**
	 * 判断用户是否充值
	 * @param iccid
	 * @return
	 */
	public String chongzhi(String iccid) {
		int n = GetTerminalRatingClient.GetTerminalRating(iccid);
		System.out.println("number : "+n);
		if(n > 1){
			return "yes";
		}else{
			return "no";
		}
	}
}
